<?php

namespace Admin\Controller;

use Think\Controller;
use Think\Page;

class NodeController extends Controller
{
    public function all()
    {
      	// 读取所有数据
      	if(!IS_GET) return fasle;
		$_data = M('node')->where('')->order('')->select();
      	$this->ajaxReturn($_data,'json');
    }
    public function index()
    {
        // 读取数据
        if(!IS_GET) return fasle;
      	$m = M('node');
      	$count = $m->count();
        $_data = $m->where('')->order('')->page($_GET['page'], 15)->select();
        if(empty($_data)) return false;
      	$_result = array(
        	'node_info' => $_data,
          	'page' => $count,
        );
      	$this->ajaxReturn($_result,'json');
    }

    public function add()
    {
        // 添加数据
        if(!IS_POST) return false;
        $node = $_POST;
        if(empty($node)) return false;
        foreach($node as $k=>$v) {
            //
        }
        $result = M('node')->add($node);
        //$this->ajaxReturn(array('code'=>$result),'json');
        echo json_encode(array('code'=>$result));
    }

    public function edit()
    {
        // 编辑数据
        if(IS_GET) {
            if(!isset($_GET['id']) || empty($_GET['id'])) return false;
            $find = M('node')->where("{$_GET['id']} = n_id")->find();
            $this->ajaxReturn($find,'json');
        } else if(IS_POST) {
            // 如果更新成功，则删除原先那条数据，然后追加 （前端）
            if(!isset($_POST['n_id']) || !is_numeric($_POST['n_id'])) return false;
            $n_id = $_POST['n_id'];
            unset($_POST['n_id']);
            $update = M('node')->where("{$n_id} = n_id")->save($_POST);
            echo json_encode(array('code'=>$update));
        }
    }

    public function del()
    {
        // 删除数据
        if(!isset($_POST['id']) || empty($_POST['id'])) return false;
        $del = M('node')->delete($_POST['id']);
        //$this->ajaxReturn(array('code'=>$del),'json');
        echo json_encode(array('code'=>$del));
    }
}